package com.hjq.fcloud.ui.activity

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.hjq.fcloud.R
import com.hjq.fcloud.bean.CognovitActionemDetailBean
import com.hjq.fcloud.bean.FlowDataBean
import com.hjq.fcloud.config.FlowStatusConfig
import com.hjq.fcloud.event.EventBean
import com.hjq.fcloud.event.EventBusUtils
import com.hjq.fcloud.http.api.CognovitActionemDetailApi
import com.hjq.fcloud.http.api.CognovitActionemSubmitApi
import com.hjq.fcloud.http.model.HttpData
import com.hjq.fcloud.ktx.ViewKtx.setVisibility
import com.hjq.fcloud.jump.CommonJumpBean
import com.hjq.fcloud.other.AppFilesHelper
import com.hjq.fcloud.widget.BottomSignViewClickListener
import com.hjq.http.EasyHttp
import com.hjq.http.body.JsonBody
import com.hjq.http.listener.HttpCallback
import kotlinx.android.synthetic.main.activity_cognovit_actionem.*
import kotlinx.android.synthetic.main.activity_platform_service_fee_sign.bottomView
import kotlinx.android.synthetic.main.activity_platform_service_fee_sign.feedbackRootView
import kotlinx.android.synthetic.main.activity_platform_service_fee_sign.flowView
import kotlinx.android.synthetic.main.activity_platform_service_fee_sign.signModeSb
import kotlinx.android.synthetic.main.layout_sign_result.signResultIv
import okhttp3.Call
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 债务确认签约
 */
class CognovitActionemActivity : BaseDetailActivity() {

    companion object {
        //1通过0不通过
        const val STATUS_PASS = "1"
        const val STATUS_NOT_PASS = "0"

        fun start(ac: Context, jumpBean: CommonJumpBean,isDisableAgainSign:Boolean = false) {
            var intent = Intent(ac, CognovitActionemActivity::class.java)
            intent.putExtra(INTENT_IN_ITEM, jumpBean)
            intent.putExtra(INTENT_IN_DISABLE_AGAIN_SIGN, isDisableAgainSign)
            ac.startActivity(intent)
        }
    }

    private var mDetail: CognovitActionemDetailBean? = null
    private var status: String? = STATUS_PASS

    override fun userEventBus(): Boolean {
        return true
    }

    override fun getStateLayout(): View? {
        return statusLayout
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_cognovit_actionem
    }

    override fun initView() {

    }

    override fun initData() {
        getServerFlow()

        getServerFlowStatus({ flowStatusBean ->
            if(flowStatusBean.isFlowUnfinished){
                showFlowUnfinishedView(flowStatusBean.flowUnfinishedTipsArray)
                return@getServerFlowStatus
            }

            getServerDetail()

            if (flowStatusBean.canSign || flowStatusBean.canAgainSign) {
                var isAgainSign = flowStatusBean.canAgainSign && !isDisableAgainSign//重新签约
                if (isAgainSign) {//需要展示流程和签约方式
                    flowView.setVisibility(true)
                    //签约方式
                    signModeSb.setVisibility(true)
                } else {
                    //审核控件设置
                    feedbackRootView.setVisibility(true)
                }
                //底部签约控件设置
                bottomView.setViewData(isAgainSign, object :
                    BottomSignViewClickListener {
                    override fun onAgainPassSign() {
                        requestServerResetFlow() {
                            start(context, commonJumpBean!!, true)
                            finish()
                        }
                    }

                    override fun onPassSign() {
                        status = STATUS_PASS
                        if (checkInput()) {
                            FaceDetectRGBActivity.launcher(context, intent.extras)
                        }
                    }

                    override fun onNoPassSign() {
                        status = STATUS_NOT_PASS
                        if (checkInput()) {
                            FaceDetectRGBActivity.launcher(context, intent.extras)
                        }
                    }

                })
            } else {
                flowView.setVisibility(true)
                //签约方式
                signModeSb.setVisibility(true)
            }
        })
    }

    private fun getServerDetail() {
        var api = CognovitActionemDetailApi()
            .setId(commonJumpBean?.bizId)
        EasyHttp.post(this)
            .api(api)
            .request(object :
                HttpCallback<HttpData<CognovitActionemDetailBean?>?>(this) {
                override fun onStart(call: Call?) {}
                override fun onSucceed(data: HttpData<CognovitActionemDetailBean?>?) {
                    mDetail = data?.getData()
                    setViewData(mDetail)
                }
            })
    }

    override fun setFlowViewData(data: FlowDataBean?) {
        flowView.setViewData(data?.setup_list)
    }

    private fun setViewData(item: CognovitActionemDetailBean?) {
        showAppContent()

        if(!isDisableAgainSign){
            signResultIv.setViewData(flowStatusData)
        }
        noSb.setRightText(item?.code)
        projectNameSb.setRightText(item?.project?.project_name)
        sectNameSb.setRightText(item?.sect?.sect_name)
        loanSb.setRightText(item?.loanName)

        constructCorpNameSb.setRightText(item?.construct_corp?.corp_name)
        buildCorpNameSb.setRightText(item?.build_corp?.corp_name)
        supplyCorpNameSb.setRightText(item?.supply_corp?.corp_name)
        moneySb.setRightText(item?.money)
        signModeSb.setRightText(item?.debtConfirmModeName)

        var sign = bottomView.visibility == View.VISIBLE//如果是签约则取fnc-450-0，反之取fnc-450-1
        if(sign){
            fileView.setViewData(this, FlowStatusConfig.getFileTitleName(flowStatusData), item?.files?.get("fnc-450-0"))
        }else{
            fileView.setViewData(this, FlowStatusConfig.getFileTitleName(flowStatusData), item?.files?.get("fnc-450-1"))
        }
    }

    private fun checkInput(): Boolean {
        if (TextUtils.isEmpty(feedbackView.getInPutText())) {
            showToast("请输入意见反馈")
            return false
        }
        return true
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMainEventBus(bean: EventBean) {
        if (bean != null && bean.type == EventBusUtils.TYPE_FACE_AND_SIGN_FINISH) {
            var parms = bean.parms as HashMap<String, String?>
            requestServerConfirm(parms)
        }
    }

    private fun requestServerConfirm(signParms: HashMap<String, String?>) {
        var parms = HashMap<String, String?>()
        if (signParms != null) {
            parms.putAll(signParms)
        }
        parms.put("id", commonJumpBean?.bizId)
        parms.put("reason", feedbackView.getInPutText())
        parms.put("result", status)
        parms.put("biz_code", commonJumpBean?.bizCode)
        parms.put("biz_id", commonJumpBean?.bizId)
        EasyHttp.post(this)
            .api(CognovitActionemSubmitApi())
            .body(JsonBody(parms))
            .request(object : HttpCallback<HttpData<Any?>?>(this) {
                override fun onSucceed(data: HttpData<Any?>?) {
                    showToast(getString(R.string.string_sign_success_tips))
                    finish()
                }
            })
    }
}